<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Account/classes/PhpFox_ComponentAccountSettings.class.php, $Author:  $ */



/** User settings
 *
 * Parameters:
 *  1. oUser (required) - user account object
 *
 * @package module.account
 * @version $Id: PhpFox_ComponentAccountSettings.class.php 416 2008-03-28 07:20:51Z phpfoxsv $
 *
 */
class PhpFox_Mod_Account_ComponentAccountSettings extends BaseComponent
{

    /** Processing method
     */
	function process()
	{
	    $oAcc = $this->getParam('oUser');
	    /* @var $oAcc PhpFox_Mod_Account_ItemAccount */

	    assert($oAcc != null);

	    $oSrvAcc = &$this->_oModule->getService('Account');
	    /* @var $oSrvAcc PhpFox_Mod_Account_ServiceAccount */

	    $oSrvSec = &$this->_oModule->getService('Security');

	    //validator
        $aFields = array(
        	'aUser[email]' => array('title' => App::format('Mod_Account.e-mail'), 'def' => 'email'),
        );

        $aStates = array();
        if (App::getSetting('us_states'))
        {
            $aStatesArray =  App::getTxtFile('us_stat.txt', true);
			foreach($aStatesArray as $c => $d)
			{
				$aStates[$d] = $d;
			}
        }

	    $oReq = &App::getRequest();
	    /* @var $oReq Request */

  		$aUserData =  $oReq->getArray('aUser');

		if ( !empty($aUserData['zip']) )
		{
			$aFields['aUser[zip]']   = array('title' => App::format('Mod_Account.zip'),   'def' => 'zip_us');
		}

		$oValidator = App::newObject('Validator', $aFields);
  		/* @var $oValidator Validator */

	    // processing
        switch($oReq->getAction())
        {
            case 'update':

                $oldLang = $oAcc->aData['lang_file'];
				$sOldEmail = App::getUser('email');
				$aUserData['day']      = $oReq->getInt('aBirthDate_Day');
				$aUserData['month']    = $oReq->getInt('aBirthDate_Month');
				$aUserData['year']     = $oReq->getInt('aBirthDate_Year');
				$aUserData['p_update'] = time();
				$oAcc->setData($aUserData);
				if ($oValidator->isValid($oReq->getAll()))
				{
					$oBanService = &App::getModuleService('Site', 'Ban');
					if (!$oBanService->checkEmail($oAcc->aData['email']))
					{
					    $this->addErrors(App::format('Mod_Site.info.email_banned'));
					}				    
					else 
					{
						if ($oSrvAcc->checkEmailUnique($aUserData['email'], $oAcc->aData['user']))
					    {
					    	$aUpdFields = array('email', 'gender', 'location', 'city',
	    				                        'day', 'month', 'year',
	    				                        'not_1', 'not_2', 'not_3', 'not_4', 'not_5',
	    				                        'friends_only', 'friends_comment', 'setting_gb_html',
	    				                        'zip', 'state',
	    				                        'p_update',
	    				                        'lang_file',
	    				                        );
	    				    if ($oAcc->update($aUpdFields))
	    				    {
	    				    	if (App::getSetting('signup_email') && ($aUserData['email'] != $sOldEmail))
	    				    	{
	    				    		$sVerifyCode = time();
	    				    		$oDb = Database::get();
	    				    		$oDb->update(App::getT('user'), array('verify' => '1', 'verifycode' => $sVerifyCode), 'id = "' . App::getUser('id') . '"');

	    				    		$oMailer = &App::newObject('mail_Mailer');
						    		$sVerifyLinkParams = array(
						    			'id'   => App::getUser('id'),
						    		    'code' => md5(App::getUser('password')),
						    		    'verify' => md5($sVerifyCode)
						    		);
						    		$aVars = array(
						    			'SITENAME'    => App::getSetting('site_name'),
						    			'USERNAME'    => App::getUser('user'),
						    		    'VERIFY_LINK' => App::makeUrl('public.verify', $sVerifyLinkParams),
						    		);
							
						    		$oMailer->send('email_reverify', $aUserData['email'],
						    		               App::format('Mod_Account.verify_email'), $aVars);

									$oSrvSec->logout();             
							        App::gotoUrl('public.login', array('new' => 'account', 'verify' => 'yes', 'pass' => 'update'));
	    				    	}

	    				    	App::plugin_updateAccount($oReq);
	
	    				    	if ( isset($aUserData['lang_file']) && ($oldLang != $aUserData['lang_file']) )
	    				    	{
	    				    		$oLang = &App::getLanguage();
	    				    		$oLang->removeCachedFiles();
	    				    		$oLang->loadLanguages();
	    				    	}
	
	    				    	App::rmCache('featured_members.php');
	
	    				        $oUrl = &App::getUrl();
								App::gotoUrl('public.account.settings.account', array(), App::format('Mod_Account.account.updated'), false);
	    				    }
	    				    else
	    				    {
	    				        $this->addErrors($oAcc->getErrors());
	    				    }
	    				 }
	    				 else
	    				 {
					        $this->addErrors($oSrvAcc->getErrors());
	    				 }
					}
				}
				else
				{
				    $this->addErrors($oValidator->getErrors());
				}

				break;
        }

        $dtBirthdate = $oAcc->aData['year'] . '-' . $oAcc->aData['month'] . '-' . $oAcc->aData['day'];
		$oTpl = &$this->_oModule->getTemplate();
		$oTpl->assignSrc(array(
		    'aUser'        => $oAcc->aData,
			'dtBirthdate'  => $dtBirthdate,
			'aCountries'   => App::getOptions('country'),
			'aGenders'   => App::getOptions('gender'),
	        'bZipState'    => App::getSetting('us_states'),
		    'aStates'      => $aStates,
	        'jsValidation' => $oValidator->makeJS(),
			'jsValMess'    => $oValidator->makeJsMess(),
			'aErrors'      => $this->getErrors(),
            'bCanCancel'   => App::getSetting('member_delete'),
		    'iStartYear'   => App::getSetting('birthdate_year_min'),
		    'iEndYear'     => App::getSetting('birthdate_year_max'),
			'bCrush' => !($oSrvSec->checkAccess('crush', '', false)) ? false :true,
			'aLanguages' => App::getLanguages(),
			'bAllowLangPack' => App::getSetting('allow_change_lang'),
			'bDone' => $oReq->get('updated'),
		));
		$srcHtml = $oTpl->fetch('AccountSettings.html');
		return $srcHtml;
	}

}
?>